package com.runa.rccprotocol.dto.daoheng;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

/**
 * @author diandian
 * @Date 2021/11/1 9:53
 */
public class DaoHengRequestData extends ConcentratorRequestData {
    public DaoHengRequestData(String address) {
        super(address);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return requestData;
    }

    private byte[] requestData;

    public void readHouseValve(String valve) {
        requestData = new byte[]{0x68, (byte) 0xA3, 0, 0, 0, 0, 0x55, 0x55, 0x55, 0x01, 0x03, (byte) 0x90, 0x1D, 0x01, 0,0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        setHaveResult(true);
    }

    public void houseValveOpen(String valve, int open) {
        requestData = new byte[]{0x68, (byte) 0xA3, 0, 0, 0, 0, 0x55, 0x55, 0x55, 0x04, 0x04, (byte) 0xA0, 0x22, 0x01, (byte) open,0,0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[15] = ByteUtil.sum(requestData, 0, 15);
        setHaveResult(true);
    }

    public void houseValveOpen2(String valve, boolean open) {
        byte value = (byte) (open ? 0x55 : 0x99);
        requestData = new byte[]{0x68, (byte) 0xA3, 0, 0, 0, 0, 0x55, 0x55, 0x55, 0x55, 0x04, 0x04, (byte) 0xA0, 0x10, 0x01, value,0,0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 2, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 15);
        setHaveResult(true);
    }

    public void readElectric(String meterNo) {
        requestData = new byte[]{0x68,0,0,0,0,0,0,0x68,0x11,0x04,0x33,0x33,0x33,0x33,0,0x16};
        ByteUtil.tenstringToBcd(requestData, meterNo, 1, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        setHaveResult(true);
    }

    public static void main(String[] args) {
        DaoHengRequestData requestData = new DaoHengRequestData(null);
        requestData.readHouseValve("12345678");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.houseValveOpen("12345678",50);
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.readElectric("20210136");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
        byte[] requestData1 = new byte[]{0x68,0,0,0,0,0,0,0x68,0x11,0x04,0x33,0x33,0x33,0x33,0,0x16};
        ByteUtil.tenstringToBcd(requestData1, "210816075747", 1, false);
        requestData1[14] = ByteUtil.sum(requestData1, 0, 14);
        System.out.println(ByteUtil.byteToHexString(requestData1));
    }

}